<template>
  <!-- 资讯评论审核 -->
  <div>
    <!-- 头部操作行 -->
    <el-row :gutter="15">
      <el-col :span="1.5"
        ><el-button type="danger" @click="deleteBatch"
          >批量删除</el-button
        ></el-col
      >
      <el-col :span="5"
        ><el-input
          v-model="params.keywords"
          placeholder="请输入关键字"
          clearable
          @clear="clearInput"
          @keyup.enter.native="enterInput"
      /></el-col>
      <el-col :span="3"
        ><el-button type="primary" @click="queryInfo">查询</el-button></el-col
      >
    </el-row>
    <!-- 表格部分 -->
    <el-row class="tablePart">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <el-table-column label="序号" type="index" />
        <el-table-column
          align="center"
          header-align="center"
          prop="comment"
          label="评论内容"
        />
        <el-table-column
          align="center"
          header-align="center"
          prop="userId"
          label="评论人编号"
          width="100"
        />
        <el-table-column
          align="center"
          header-align="center"
          prop="articleId"
          label="资讯编号"
          width="100"
        />
        <el-table-column
          align="center"
          header-align="center"
          prop="commentTime"
          label="评论时间"
          :formatter="fmtTableDateDay"
        />
        <el-table-column
          align="center"
          header-align="center"
          prop="status"
          label="评论状态"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === '通过'" type="success"
              >通过</el-tag
            >
            <el-tag v-else-if="scope.row.status === '不通过'" type="danger"
              >不通过</el-tag
            >
            <el-tag v-else type="info">未审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              type="text"
              style="color: #e6a23c"
              @click="deleteRow(scope.row.id)"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.status === '未审核'"
              type="text"
              @click="passComment(scope.row.id)"
              >通过</el-button
            >
            <el-button
              v-if="scope.row.status === '未审核'"
              style="color: #f56c6c"
              type="text"
              @click="noPassComment(scope.row.id)"
              >不通过</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!-- 分页 -->
    <el-pagination
      class="pagination_right"
      :current-page="params.page"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="params.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { pageQuery, deleteById, batchDelete, check } from "@/api/infoComment";
import { fmtTableDateDay } from "@/filters/index";

export default {
  data() {
    return {
      // 时间格式
      fmtTableDateDay,
      // 查询参数
      params: {
        page: 1,
        pageSize: 10,
        keywords: "",
      },
      // 数据总数
      total: 0,
      // 表格数据--数组
      tableData: [],
      // 选中的id数组
      ids: [],
    };
  },
  computed: {},
  created() {
    this.getData();
  },
  mounted() {},
  methods: {
    // 获取数据
    async getData() {
      const { data: res } = await pageQuery({ ...this.params });
      this.tableData = res.list;
      // console.log(this.tableData);
      this.total = res.total;
    },
    // 改变页面数据数量
    handleSizeChange(newPageSize) {
      this.params.pageSize = newPageSize;
      this.getData();
    },
    // 改变当前页面 页码
    handleCurrentChange(newPage) {
      this.params.page = newPage;
      this.getData();
    },
    // 点击查询按钮 查询数据
    async queryInfo() {
      // console.log(this.params);
      if (this.params.keywords === "") {
        return this.getData();
      }
      this.getData();
    },
    handleSelectionChange(res) {
      this.ids = res.map((item) => {
        return item.id;
      });
      // console.log(this.ids);
    },
    // 清空触发事件
    clearInput() {
      this.getData();
    },
    // 按回车触发事件
    enterInput() {
      this.queryInfo();
    },
    // 删除一行
    deleteRow(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const res = await deleteById({ id: id });
        // console.log(res);
        if (res.status === 200) {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getData();
        } else {
          this.$message({
            type: "danger",
            message: "删除失败!",
          });
        }
      });
    },
    // 批量删除
    deleteBatch() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (this.ids.length === 0) return this.$message.error("参数异常");
          const res = await batchDelete({ ids: this.ids });
          if (res.status !== 200) return this.$message.error("删除失败");
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getData();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 点击通过
    async passComment(id) {
      const res = await check({ id: id, status: "通过" });
      if (res.status !== 200) return this.$message.error("更新失败");
      this.getData();
    },
    // 点击不通过
    async noPassComment(id) {
      const res = await check({ id: id, status: "不通过" });
      if (res.status !== 200) return this.$message.error("更新失败");
      this.getData();
    },
  },
};
</script>
<style scoped lang="scss">
.tablePart {
  margin-top: 10px;
}
.pagination_right {
  float: right;
  margin-top: 8px;
}
</style>
